package com.gongpingjialoan.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.gongpingjialoan.R;
import com.gongpingjialoan.activity.sell.SellPrivateCarDetailActivity;
import com.gongpingjialoan.bean.PriCarPhotoItem;

import java.util.List;

/**
 * Created by Administrator on 2016/9/7.
 */
public class PhotoGridAdapter extends BaseAdapter {

    private List<PriCarPhotoItem> mPriCarPhotoItems;

    private Context mContext;

    private LayoutInflater mLayoutInflater;

    private boolean isEdit = false;

    public PhotoGridAdapter(List<PriCarPhotoItem> priCarPhotoItems, Context context , boolean isEdit) {
        mPriCarPhotoItems = priCarPhotoItems;
        mContext = context;
        this.isEdit = isEdit;
        mLayoutInflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return mPriCarPhotoItems.size();
    }

    @Override
    public Object getItem(int position) {
        return mPriCarPhotoItems.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, final ViewGroup parent) {
        final ViewHolder holder;
        if (convertView == null) {
            convertView = mLayoutInflater.inflate(
                    R.layout.photo_item, null);
            holder = new ViewHolder();
            holder.titleTextView = (TextView) convertView.findViewById(R.id.title);
            holder.imageView = (ImageView) convertView.findViewById(R.id.car_img);
            holder.colseImageView = (ImageView) convertView.findViewById(R.id.imgView_delete);
            convertView.setTag(holder);
        }else{
            holder = (ViewHolder) convertView.getTag();
        }

        String url = mPriCarPhotoItems.get(position).getImgUrl();
        if(isEdit && !TextUtils.isEmpty(url)){
            holder.colseImageView.setVisibility(View.VISIBLE);
        }else{
            holder.colseImageView.setVisibility(View.GONE);
        }
        holder.titleTextView.setText(mPriCarPhotoItems.get(position).getTitle());
        Glide.with(mContext).load(url).placeholder(R.drawable.add_photo).centerCrop().into(holder.imageView);
        holder.colseImageView.setTag(mPriCarPhotoItems.get(position));
        holder.colseImageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(getImageCount() <= 1 ) {
                    Toast.makeText(mContext, "必须保留一张图片", Toast.LENGTH_SHORT).show();
                    return;
                }
                PriCarPhotoItem item = (PriCarPhotoItem) v.getTag();
                item.setImgUrl("");
                holder.imageView.setImageResource(R.drawable.add_photo);
                holder.imageView.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
                SellPrivateCarDetailActivity offSellPrivateCarDetailActivity = (SellPrivateCarDetailActivity) mContext;
                if(offSellPrivateCarDetailActivity.mOffSellPrivateCarDetailFragment.isAdded()){
                    offSellPrivateCarDetailActivity.mOffSellPrivateCarDetailFragment.deleteImg(parent ,item , PhotoGridAdapter.this);
                }

            }
        });
        return convertView;
    }


    private int getImageCount(){
        int num = 0;
        int size = getCount();
        for (int i = 0; i < size; i++) {
            PriCarPhotoItem item = mPriCarPhotoItems.get(i);
            if(!TextUtils.isEmpty(item.getImgUrl())){
                num++;
            }
        }
        return num;
    }



    public static class ViewHolder {
        public TextView titleTextView;
        public ImageView imageView;
        public ImageView colseImageView;
    }
}
